package com.example.light.common.util;

import com.example.light.common.exception.GlobalException;
import com.example.light.common.result.CodeMsg;
import com.example.light.web.personal.vo.FileVO;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @Author: lizhilong
 * @Description: 文件上传工具类
 * @Date: Created in 19:57 2018/8/31
 * @Modified By:
 */
public class UploadFileUtil {

    public static void catFile(MultipartFile file, String filename, String filePath) {
        try {
            File targetFile = new File(filePath);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            byte[] fileByte = file.getBytes();
            FileOutputStream cat = new FileOutputStream(filePath + filename);
            cat.write(fileByte);
            cat.flush();
            cat.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException(CodeMsg.FileCodeMsg.NOT_CAN_CREATEFILE);
        }
    }

//    public static ArrayList<FileVO> getAllFileName(String path) {
//        File file = new File(path);
//        ArrayList<FileVO> files = new ArrayList<FileVO>();
//        File[] filesPath = file.listFiles();
//        for (File f : filesPath) {
//               if (f.isDirectory()){
//                    files.add(new FileVO(f.getName(),"folder", (int) f.length()));
//               }
//        }
//    }

}

